package com.ly.jwt.controller;


import com.ly.jwt.config.RolePermission;
import com.ly.jwt.db.entity.UserAccount;
import com.ly.jwt.service.UserAccountService;
import com.ly.jwt.utils.AuthorityUtils;
import com.ly.jwt.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 账号的
 */
@RestController
@RequestMapping("/user")
public class UserAccountController {
    @Autowired
    UserAccountService userAccountService;

    /**
     * 登录
     * @param userAccount
     * @json_param {"username": "admin","password": "123456"}
     * @return
     */
    @PostMapping(value = "/login")
    public JsonResult<?> login(@RequestBody UserAccount userAccount) {
        return userAccountService.login(userAccount);
    }

    /**
     * 注册账号
     * @param userAccount
     * @json_param {"username": "admin1","password": "123456"}
     * @return
     */
    @PostMapping(value = "/insert")
    public JsonResult<?> insertSelective(@RequestBody UserAccount userAccount) {
        return userAccountService.insertSelective(userAccount);
    }

    /**************************************************测试权限**************************************************/

    @RolePermission(login = false)
    @PostMapping(value = "/selectAll")
    public JsonResult<?> selectAll(HttpServletRequest request) {
        List<UserAccount> userAccounts = userAccountService.selectAll();
        return new JsonResult<>(userAccounts);
    }

    @PostMapping(value = "/selectAll1")
    public JsonResult<?> selectAll1(HttpServletRequest request) {
        List<UserAccount> userAccounts = userAccountService.selectAll();
        return new JsonResult<>(userAccounts);
    }

    @RolePermission(role = AuthorityUtils.USER2)
    @PostMapping(value = "/selectAll2")
    public JsonResult<?> selectAll2(HttpServletRequest request) {
        List<UserAccount> userAccounts = userAccountService.selectAll();
        return new JsonResult<>(userAccounts);
    }

}
    
